package com.naiterui.ehp.ps.push.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.ps.push.exception.ExceptionCodes;
import com.naiterui.ehp.ps.push.service.IMessageSendService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 消息发送
 *
 * @since 2.3.0
 */
@RestController
@AllArgsConstructor
@RequestMapping("push/send")
@Api(tags = {SwaggerTag.PUSH + SwaggerTag.MESSAGE_SEND})
public class MessageSendController {

    /**
     * 日志对象 ：<code>logger</code>
     */
    private static final Logger logger = LoggerFactory.getLogger(MessageSendController.class);

    private final IMessageSendService messageSendService;

    /**
     * 批量消息发送
     * MessageSendController.batchSend()
     *
     * @param requestUri
     * @param info
     *
     * @return
     *
     * @Date 2016年4月6日
     * @since 2.3.0
     */
    @RequestMapping("batch/{requestUri}")
    public void batchSend(@PathVariable String requestUri, String info) throws BusinessException {
        // 空参校验
        if (StringUtils.isEmpty(info)) {
            logger.error("batchSend ERROR ; message info is empty : requestUri={},info={}", requestUri, info);
            throw new BusinessException(ExceptionCodes.MESSAGE_TARGET_OR_CONTENT_EMPTY);
        }
        this.messageSendService.sendMessage(requestUri, info);
    }

    /**
     * 批量发送私人主题通知
     */
    @RequestMapping("group/{requestUri}")
    public void groupSendNotice(@PathVariable String requestUri, String recipientCacheKey, String info) throws BusinessException {
        // 空参校验
        if (StringUtils.isEmpty(info)) {
            logger.error("groupSendNotice ERROR ; message info is empty : requestUri={},info={}", requestUri, info);
            throw new BusinessException(ExceptionCodes.MESSAGE_TARGET_OR_CONTENT_EMPTY);
        }
        this.messageSendService.sendMessage(requestUri, recipientCacheKey, info);
    }

    /**
     * 单条消息发送
     */
    @RequestMapping("single/{requestUri}")
    public void singleSend(@PathVariable String requestUri, String info, Long userId) throws BusinessException {
        // 空参校验
        if (StringUtils.isEmpty(info) || null == userId) {
            logger.error("singleSend ERROR ; message info is empty : requestUri={},info={},userId={}", requestUri, info, userId);
            throw new BusinessException(ExceptionCodes.MESSAGE_TARGET_OR_CONTENT_EMPTY);
        }
        // UserType 待处理
        this.messageSendService.sendMessage(requestUri, userId, info);
    }

}
